package yc.star.friend.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import yc.star.common.constants.Constant;
import yc.star.common.domain.AppResult;
import yc.star.common.security.exception.ApplicationException;
import yc.star.common.utils.ThreadLocalUtil;
import yc.star.friend.domain.user.vo.UserVO;
import yc.star.friend.manager.UserCacheManager;

import java.util.Objects;

@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    @Before(value = "@annotation(yc.star.friend.aspect.CheckUserStatus)")
    public void before(JoinPoint point){
        Long userId = ThreadLocalUtil.get(Constant.USER_ID, Long.class);
        UserVO user = userCacheManager.getUserById(userId);
        if (user == null) {
            throw new ApplicationException(AppResult.failed("用户不存在"));
        }
        if (Objects.equals(user.getStatus(), Constant.FALSE)) {
            throw new ApplicationException(AppResult.failed("您已被列入黑名单"));
        }
    }
}